import { getHotCity, getCityPosition } from "../../apis/index";
// pages/selectCity/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hotCity: [],
    latitude: '',
    longitude: '',
    location: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    // 获取热门城市
    this.getHotCityData()
  },
  // 选择热门城市
  selectCity(e){
    const {name} = e.currentTarget.dataset
    this.setData({
      location: name
    })
    wx.setStorageSync('cityName', name)
    wx.switchTab({
      url: '/pages/food/index'
    })
  },
  // 获取当前城市
  getCurrentCity() {
    wx.getLocation({
      type: 'wgs84',
      success: async (res) => {
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        // 调用接口，获取当前城市
        const { latitude, longitude } = this.data
        const rs = await getCityPosition(latitude, longitude)
        this.setData({
          location: rs.result.ad_info.city
        })
        const cityName =  this.data.location.slice(0, 2)
        // cityName存储到本地当中
        wx.setStorageSync('cityName', cityName)
        wx.switchTab({
          url: '/pages/food/index'
        })
      },
      fail(err) {
        console.error('获取位置失败:', err);
      }
    });
  },
  // 获取热门城市
  async getHotCityData() {
    const res = await getHotCity()
    if (res.status === 200) {
      this.setData({
        hotCity: res.data
      })
    }
  }
})